/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>管理员管理</h3>
        <h5>网站系统管理员列表</h5>
      </div>
    </div>

    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>网站系统管理员, 由总平台设置管理.</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>管理员列表</h3>
      </div>
      <a @click="toAdd()">
        <div class="fbutton">
          <div title="新增管理员" class="add">
            <span>
              <i class="el-icon-circle-plus"></i> 新增管理员
            </span>
          </div>
        </div>
      </a>
      <a class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-input class="searchinput" v-model="user_name" size="small" placeholder="用户名" clearable></el-input>
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>

    <div class="tablebox">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="ID" width="180" prop="admin_id" show-overflow-tooltip></el-table-column>
        <el-table-column label="用户名" width="180" prop="user_name" show-overflow-tooltip></el-table-column>
        <el-table-column label="email" width="180" prop="email" show-overflow-tooltip></el-table-column>
        <el-table-column label="email" width="180" show-overflow-tooltip>
          <template slot-scope="scope">{{scope.row.add_time | datefmt}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" @click="toEdit(scope)" class="el-icon-edit-outline">编辑</el-button>
              <el-button
                size="small"
                @click="delFn(scope.row.admin_id)"
                class="el-icon-delete"
                v-if="scope.row.admin_id > 1"
              >删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
    <el-dialog
      title="添加/编辑管理员"
      :visible.sync="dialogFormVisible"
      width="22%"
      center
      :modal-append-to-body="false"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="dialog-form"
        label-width="120px"
      >
        <el-form-item label="用户名" prop="user_name">
          <el-input v-model="ruleForm.user_name" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="Email地址" prop="email">
          <el-input v-model="ruleForm.email" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="登陆密码" prop="newpassword">
          <el-input v-model="ruleForm.newpassword" type="password" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="确认登录密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSubmit" :loading="isLoading">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAdminPage,
  getAdmin,
  addAdmin,
  updateAdmin,
  deleteAdmin
} from "@/utils/api";
import md5 from "@/utils/md5.js";
export default {
  data() {
    var checkUserName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        callback();
      }
    };
    var validateNewPassword = (rule, value, callback) => {
      if (value === "" || value === undefined) {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "" || value === undefined) {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      rules: {
        user_name: [
          {
			required: true,
            validator: checkUserName,
            trigger: "blur"
          }
        ],
        email: [
          {
			required: true,
            validator: validateEmail,
            trigger: "blur"
          }
        ],
        newpassword: [
          {
			required: true,
            validator: validateNewPassword,
            trigger: "blur"
          }
        ],
        password: [
          {
			required: true,
            validator: validatePassword,
            trigger: "blur"
          }
        ]
      },
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      user_name: "",
      dialogFormVisible: false,
      ruleForm: {
        user_name: "",
        email: "",
        password: ""
      },
      formLabelWidth: "120px",
      isLoading: false
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      getAdminPage({
        page: this.currentPage,
        size: this.pageSize,
        user_name: this.user_name
      }).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    delFn(id) {
      var that = this;
      this.$confirm("是否删除该管理员?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteAdmin(id).then(function(res) {
            that.$message.success("删除成功!");
            that.getPage();
          });
        })
        .catch(() => {
          that.$message.info("已取消删除");
        });
    },
    toEdit(scope) {
      var that = this;
      getAdmin(scope.row.admin_id).then(function(res) {
        that.ruleForm = res;
        that.dialogFormVisible = true;
      });
    },
    confirmSubmit() {
      var that = this;
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.isLoading = true;
          var requestForm = {};
          if (that.ruleForm.admin_id) {
            requestForm = {
              admin_id: that.ruleForm.admin_id,
              user_name: that.ruleForm.user_name,
              email: that.ruleForm.email,
              password: md5("TPSHOP" + that.ruleForm.password)
            };
            updateAdmin(requestForm).then(function(res) {
              if (res.status == 1) {
                that.$message.success({
                  message: "编辑成功!",
                  onClose: function() {
                    that.dialogFormVisible = false;
                    that.getPage();
                  }
                });
              } else {
                that.$message.error(res.msg);
              }
              that.isLoading = false;
            });
          } else {
            requestForm = {
              user_name: that.ruleForm.user_name,
              email: that.ruleForm.email,
              password: md5("TPSHOP" + that.ruleForm.password)
            };
            addAdmin(requestForm).then(function(res) {
              if (res.status == 1) {
                that.$message.success({
                  message: "添加成功!",
                  duration: 1000,
                  onClose: function() {
                    that.dialogFormVisible = false;
                    that.getPage();
                  }
                });
              } else {
                that.$message.error(res.msg);
              }
              that.isLoading = false;
            });
          }
        } else {
          that.$message.error("参数错误");
          return false;
        }
      });
    },
    toAdd() {
      this.ruleForm = {
        user_name: "",
        email: "",
        newpassword: "",
        password: ""
      };
      this.dialogFormVisible = true;
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
>>> .dialog-form .el-input__inner {
  width: 200px;
}
</style>
